package com.A21.Adapter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.A21.Object.MSTProduct;
import com.A21MobileSales.R;

public class ProductListAdapter extends BaseAdapter {
	ViewHolder holder;
	LayoutInflater inflater;
	ArrayList<MSTProduct> lstProducts;
	ArrayList<MSTProduct> mOriginalValues;
	static final String KEY_Product = "Product";
	static final String KEY_PName = "ProductName";
	private Context context;

	public ProductListAdapter() {
		// TODO Auto-generated constructor stub
	}

	public ProductListAdapter(Context _context,
			ArrayList<MSTProduct> _lstProducts) {
		Activity act = (Activity) _context;
		this.context = _context;
		this.lstProducts = _lstProducts;
		this.mOriginalValues = new ArrayList<MSTProduct>();
		this.mOriginalValues.addAll(lstProducts);
		inflater = (LayoutInflater) act
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return lstProducts.size();
	}

	public Object getItem(int arg0) {
		return lstProducts.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.row_product_selection, null);
			holder = new ViewHolder();
			holder.txtProduct = (TextView) vi
					.findViewById(R.id.PSLI_txtProduct);
			holder.txtPName = (TextView) vi.findViewById(R.id.PSLI_txtPName);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}
		try {
			String line1 = "<b>" + lstProducts.get(position).Product + "</b>";
			holder.txtProduct.setText(Html.fromHtml(line1));
			//String strEncoded="";
			if(lstProducts.get(position).PName==null){ 
				lstProducts.get(position).PName="";
			} 
			//strEncoded=new String(lstProducts.get(position).PName.getBytes(Charset.forName("iso-8859-1")), Charset.forName("UTF-8")); 
			//holder.txtPName.setText(strEncoded);
			holder.txtPName.setText(lstProducts.get(position).PName);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return vi;
	}

	private class ViewHolder {
		TextView txtProduct;
		TextView txtPName;
	}

	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		lstProducts.clear();
		if (charText.length() == 0) {
			lstProducts.addAll(mOriginalValues);
		} else {
			for (MSTProduct pc : mOriginalValues) {
				final String Product = pc.Product;
				final String ProductName = pc.PName;
				if (Product.toLowerCase().contains(charText)
						|| ProductName.toLowerCase().contains(charText)) {
					lstProducts.add(pc);
				}
			}
		}
		notifyDataSetChanged();
	}

	public void filterByBMM(String brand, String model, String made,
			String charText) {
		// charText = charText.toLowerCase(Locale.getDefault());
		brand = brand.toLowerCase(Locale.getDefault());
		model = model.toLowerCase(Locale.getDefault());
		made = made.toLowerCase(Locale.getDefault());
		lstProducts.clear();
		if (brand.length() == 0 && model.length() == 0 && made.length() == 0
				&& charText.length() == 0) {
			lstProducts.addAll(mOriginalValues);
		} else {
			for (MSTProduct pc : mOriginalValues) {
				final String Brand = pc.Brand == null ? "" : pc.Brand;
				final String Model = pc.Model == null ? "" : pc.Model;
				final String Made = pc.Made == null ? "" : pc.Made;
				final String Product = pc.Product;
				final String ProductName = pc.PName;
				//brand=brand.equals("")? Brand : brand ; 
				//model=model.equals("")? Model : model ; 
				//made=made.equals("")? Made : made ; 
				
				//if (!charText.equals("")) {
					if ((Product.toLowerCase().contains(charText) || ProductName.toLowerCase().contains(charText)) && Brand.toLowerCase().contains(brand)
							&& Model.toLowerCase().contains(model)
							&& Made.toLowerCase().contains(made)) {
						lstProducts.add(pc);
					}
				//} 
				/*else {
					if (Brand.toLowerCase().contains(brand)
							|| Model.toLowerCase().contains(model)
							|| Made.toLowerCase().contains(made)) {
						lstProducts.add(pc);
					}
				}  */
			}
		}
		notifyDataSetChanged();
	}
	 

}